using System;
using System.IO;
using System.Windows.Forms;
using static System.Windows.Forms.VisualStyles.VisualStyleElement;

namespace WinFormsApp2
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            InitializeFileSystemTree();
            InitializeListViewColumns();
            this.treeView1.AfterSelect += new TreeViewEventHandler(this.treeView1_AfterSelect);
            this.listView1.DoubleClick += new EventHandler(this.listView1_DoubleClick);
        }

        private void InitializeFileSystemTree()
        {
            treeView1.Nodes.Clear();
            var rootNode = new TreeNode("This PC");
            rootNode.Tag = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile); 
            treeView1.Nodes.Add(rootNode);
            ExpandDirectory(rootNode);
        }

        private void InitializeListViewColumns()
        {
            listView1.Columns.Add("Name", 250, HorizontalAlignment.Left);  
            listView1.Columns.Add("Type", 100, HorizontalAlignment.Left);  
            listView1.Columns.Add("Size", 100, HorizontalAlignment.Right); 
            listView1.View = View.Details;  
        }

        private void ExpandDirectory(TreeNode node)
        {
            string directory = node.Tag.ToString();
            try
            {
                foreach (string dir in Directory.GetDirectories(directory))
                {
                    TreeNode newNode = new TreeNode(Path.GetFileName(dir));
                    newNode.Tag = dir;
                    node.Nodes.Add(newNode);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Error accessing directory: " + ex.Message);
            }
        }

        private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
        {
            listView1.Items.Clear();
            string directory = e.Node.Tag.ToString();
            try
            {
                foreach (string dir in Directory.GetDirectories(directory))
                {
                    ListViewItem item = new ListViewItem(Path.GetFileName(dir));
                    item.SubItems.Add("Directory");
                    item.SubItems.Add("");
                    item.Tag = dir;
                    listView1.Items.Add(item);
                }
                foreach (string file in Directory.GetFiles(directory))
                {
                    FileInfo fi = new FileInfo(file);
                    ListViewItem item = new ListViewItem(Path.GetFileName(file));
                    item.SubItems.Add("File");
                    item.SubItems.Add(fi.Length.ToString() + " bytes");
                    item.Tag = file;
                    listView1.Items.Add(item);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Error loading files: " + ex.Message);
            }
        }

        private void listView1_DoubleClick(object sender, EventArgs e)
        {
            if (listView1.SelectedItems.Count > 0)
            {
                ListViewItem item = listView1.SelectedItems[0];
                if (item.SubItems[1].Text == "File" && item.Text.EndsWith(".txt"))
                {
                    try
                    {
                        string filePath = item.Tag.ToString();
                        if (File.Exists(filePath))
                        {
                            string content = File.ReadAllText(filePath);
                            MessageBox.Show(content, "File Content", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        }
                        else
                        {
                            MessageBox.Show("File does not exist: " + filePath);
                        }
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show("Error opening file: " + ex.Message);
                    }
                }
            }
        }
    }
}
